﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.IO;
using System.ComponentModel;
using System.Reflection;

namespace BaseSet.Entity.Common.DataOperation
{
    /// <summary>
    /// 特殊符号处理
    /// </summary>
    public static class Switch
    {

        #region -- HTML 符号处理 --

        /// <summary>
        /// 将字符串中的特殊符号转换为HTML编码
        /// </summary>
        /// <param name="input">要转化的字符串</param>
        /// <returns></returns>
        public static string EncodingToHTML(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return "";
            }

            input = input.Replace("\r\n", "<br/>");
            input = input.Replace("™", "&#8482;");
            input = input.Replace("<", "&lt;");
            input = input.Replace(">", "&gt;");
            input = input.Replace("\"", "&quot;");
            input = input.Replace("©", "&copy;");
            input = input.Replace("®", "&reg;");
            input = input.Replace("￥", "&yen;");
            input = input.Replace(" ", "&nbsp;");
            input = input.Replace("&", "&amp;");

            return input;
        }

        /// <summary>
        /// 将字符串中的HTML编码转换为对应的特殊符号
        /// </summary>
        /// <param name="input">要转化的字符串</param>
        /// <returns>转化后的字符串</returns>
        public static string DecodingFromHTML(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return "";
            }
            input = input.Replace("&amp;", "&");
            input = input.Replace("&#8482;", "™");
            input = input.Replace("&lt;", "<");
            input = input.Replace("&gt;", ">");
            input = input.Replace("&quot;", "\"");
            input = input.Replace("&copy;", "©");
            input = input.Replace("&reg;", "®");
            input = input.Replace("&yen;", "￥");
            input = input.Replace("&nbsp;", " ");
            input = input.Replace("<br>", "\r\n");
            input = input.Replace("<BR>", "\r\n");

            return input;
        }

        /// <summary>
        /// 去除HTML标记
        /// </summary>
        /// <param name="strHtml">包括HTML的源码 </param>
        /// <returns>已经去除后的文字</returns>
        public static string StripHTML(string strHtml)
        {
            string[] aryReg ={
          @"<script[^>]*?>.*?</script>",

          @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
          @"([\r\n])[\s]+",
          @"&(quot|#34);",
          @"&(amp|#38);",
          @"&(lt|#60);",
          @"&(gt|#62);", 
          @"&(nbsp|#160);", 
          @"&(iexcl|#161);",
          @"&(cent|#162);",
          @"&(pound|#163);",
          @"&(copy|#169);",
          @"&#(\d+);",
          @"-->",
          @"<!--.*\n"
         
         };

            string[] aryRep = {
           "",
           "",
           "",
           "\"",
           "&",
           "<",
           ">",
           " ",
           "\xa1",//chr(161),
           "\xa2",//chr(162),
           "\xa3",//chr(163),
           "\xa9",//chr(169),
           "",
           "\r\n",
           ""
          };

            string newReg = aryReg[0];
            string strOutput = strHtml;
            for (int i = 0; i < aryReg.Length; i++)
            {
                Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
                strOutput = regex.Replace(strOutput, aryRep[i]);
            }

            strOutput.Replace("<", "");
            strOutput.Replace(">", "");
            strOutput.Replace("\r\n", "");


            return strOutput;
        }

        /// <summary>
        /// 将下载文件内容中的控制字符去掉 ？？？
        /// </summary>
        /// <param name="strChar">下载文件内容</param>
        /// <returns>返回处理后的字符串</returns>
        public static string CharTransact(string strChar)
        {
            return Regex.Replace(strChar.Replace(",", " ").Replace("，", " "), @"[\f\n\r\t\v]", string.Empty) + ",";
        }

        #endregion

        /// <summary>
        /// 将指定内容中的相对地址转为绝对地址 内容需改造
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="Url">绝对地址</param>
        /// <returns>string:转换结果</returns>
        public static string RelToAbsUrl(string content, string Url)
        {
            string strResult = content;
            strResult = Regex.Replace(strResult, "(<a[^<>]*?href=['\"])([^h.][^'\"]*)(['\"][^<>]*?>(.*?)</a>)", "$1" + Url + "$2$3", RegexOptions.IgnoreCase);
            strResult = Regex.Replace(strResult, "(<img[^<>]*?src=['\"])([^h.][^'\"]*)(['\"][^<>]*?>)", "$1" + Url + "$2$3", RegexOptions.IgnoreCase);
            strResult = Regex.Replace(strResult, "(<a[^<>]*?href=['\"])([^\\w]+)([^'\"]*)(['\"][^<>]*?>(.*?)</a>)", "$1" + Url + "$3$4", RegexOptions.IgnoreCase);
            strResult = Regex.Replace(strResult, "(<img[^<>]*?src=['\"])([^\\w]+)([^'\"]*)(['\"][^<>]*?>)", "$1" + Url + "$3$4", RegexOptions.IgnoreCase);
            return strResult;
        }

        /// <summary>
        /// 防止sql注入替换单引号
        /// </summary>
        /// <param name="input">要替换的字符串</param>
        /// <returns>替换后的字符串</returns>
        public static string ToSqlValueString(string input)
        {
            if (input == null) return "";

            return input.Replace("'", "''");
        }

        /// <summary>
        /// 数据读入textbox的转化 ？没有解码 ？应用场景
        /// </summary>
        /// <param name="strInput">要转化的字符串</param>
        /// <returns>转化后的字符串</returns>
        public static string ChgInput(string strInput)
        {
            string strValue = strInput + "";
            strValue = strValue.Replace("'", "&#39;");
            strValue = strValue.Replace("<", "&#60;");
            strValue = strValue.Replace(">", "&#62;");
            strValue = strValue.Replace("\"", "&#34;");
            return strValue;
        }
    }
}
